/*
* @Author: mz_zml
* @Date:   2018-03-28 12:17:59
* @Last Modified by:   mz_zml
* @Last Modified time: 2018-06-12 22:33:04
*/
;(function () {
  function Main () {
    this.searchVal = {
      'loadingAddressProvince': '',
      'loadingAddressCity': '',
      'destinateAddressProvince': '',
      'destinateAddressCity': '',
      'cargoTypeCode': '',
      'vechicleTypeCode': '',
      'vechicleAttributeCode': ''
    }

    this.dictList = {}
    this.transportList = {}
    this.destinateAddressList = []
    this.loadingAddressList = []
    this.initStatus = false
  }

  // 顶部按钮点击事件
  Main.prototype.headerTap = function () {
    var that = this
    $('.search-header-btn').on('click', function (e) {
      var $target = $(e.srcElement)
      // 判断是否当前选择栏
      if (!$target.hasClass('search-header-trigon-select')) {
        $('.search-header-trigon-select').removeClass('search-header-trigon-select')
        $target.addClass('search-header-trigon-select')

        if ($target.hasClass('search-header-type')) {
          $('.search-addr').css({
            'display': 'none'
          })
          $('.search-cells').fadeIn()
        }

        if ($target.hasClass('search-header-addr')) {
          // 地址选择
          // 重置到省的选项
          $('.search-addr-tab-select').removeClass('search-addr-tab-select')
          $('.search-addr-tab label').eq(0).addClass('search-addr-tab-select')
          $('.search-addr-list.show').removeClass('show').addClass('hidden')
          $('.search-addr-list').eq(0).removeClass('hidden').addClass('show')
          $('.search-cells').css({
            'display': 'none'
          })
          $('.search-addr').fadeIn()

          var html
          if ($target.hasClass('search-addr-from')) {
            html = template('tpl-province', {
              data: that.loadingAddressList
            })
          } else {
            html = template('tpl-province', {
              data: that.destinateAddressList
            })
          }
          $('.search-addr-province').html(html)
        }
      } else {
        $('.search-header-trigon-select').removeClass('search-header-trigon-select')
        $('.search-container').fadeOut()
      }
    })
  }

  // 黑色蒙版点击
  Main.prototype.maskTap = function () {
    $('.search-mask').on('click', function () {
      $('.search-container').fadeOut()
    })
  }

  // 搜索点击
  Main.prototype.searchBtnTap = function () {
    $('.search-btn').on('click', function () {
      $('.search-container').fadeIn()
      $('.search-header-trigon-select').removeClass('search-header-trigon-select')
      $('.search-header-type').addClass('search-header-trigon-select')
      $('.search-addr').css({
        'display': 'none'
      })
      $('.search-cells').fadeIn()
    })
  }

  // 地址tap点击事件
  Main.prototype.addrTabFn = function () {
    var tab = $('.search-addr-tab')
    tab.on('click', function (e) {
      var target = e.srcElement,
          $target = $(target)

      if (target.tagName.toUpperCase() == 'LABEL' && $target.index() == 0) {
        if (!$target.hasClass('search-addr-tab-select')) {
          $('.search-addr-tab-select').removeClass()
          $target.addClass('search-addr-tab-select')
          var index = $target.index()
          $('.search-addr-list.show').removeClass('show').addClass('hidden')
          $('.search-addr-list').eq(index).removeClass('hidden').addClass('show')
        }
      }
    })
  }

  // 点击双兰省市
  Main.prototype.changeAddr = function () {
    var that = this
    // 省往市跳
    $('.search-addr-province').on('click', function (e) {
      var target = e.srcElement,
          $target = $(target),
          key = $target.attr('data-key') || '',
          html = ''

      if (!$target.hasClass('search-addr-item'))
        return

      $currentObj = $('.search-header-trigon-select')

      // 如果是点击不限的话
      if ($target.text() == '不限') {
        if ($currentObj.hasClass('search-addr-to')) {
          that.searchVal.destinateAddressProvince = ''
          that.searchVal.destinateAddressCity = ''
        }

        if ($currentObj.hasClass('search-addr-from')) {
          that.searchVal.loadingAddressProvince = ''
          that.searchVal.loadingAddressCity = ''
        }

        $currentObj.html('不限')

        return
      }

      
      // $('.search-header-addr').eq(0).html(e.srcElement.innerHTML)
      $('.search-addr-tab-select').removeClass('search-addr-tab-select')
      $('.search-addr-tab label').eq(1).addClass('search-addr-tab-select')
      $('.search-addr-list.show').removeClass('show').addClass('hidden')
      $('.search-addr-list').eq(1).removeClass('hidden').addClass('show')

      if ($currentObj.hasClass('search-addr-from')) {
        that.searchVal.loadingAddressProvince = e.srcElement.innerHTML
        html = template('tpl-city', {
          data: [that.loadingAddressList[key]]
        })
      }

      if ($currentObj.hasClass('search-addr-to')) {
        that.searchVal.destinateAddressProvince = e.srcElement.innerHTML
        html = template('tpl-city', {
          data: [that.destinateAddressList[key]]
        })
      }
      $('.search-addr-city').html(html)
    })
    // 修改header市值
    $('.search-addr-city').on('click', function (e) {
      var target = e.srcElement,
          $target = $(target),
          key = $target.attr('data-key') || ''
      if (target.tagName.toUpperCase() == 'LABEL') {
        var value = e.srcElement.innerHTML
        var $currentObj = $('.search-header-trigon-select')

        $currentObj.html(value)
        if ($currentObj.hasClass('search-addr-from')) {
          that.searchVal.loadingAddressCity = key ? e.srcElement.innerHTML : ''
        }
        if ($currentObj.hasClass('search-addr-to')) {
          that.searchVal.destinateAddressCity = key ? e.srcElement.innerHTML : ''
        }
      }
    })
  }

  // 屏幕自适应
  Main.prototype.rem = function () {
    var win = window,
        doc = document,
        docEle = doc.documentElement,
        body = doc.body,
        resizeEvent = "orientationchange" in win ? "orientationchange" : "resize",
        recalc = function () {
          var clientWidth = body.offsetWidth  || docEle.clientWidth,
              clientHeight = body.offsetHeight  || docEle.clientHeight,
              isIPhone = win.navigator.appVersion.match(/iphone/gi),
              isAndroid = win.navigator.appVersion.match(/android/gi),
              devicePixelRatio = win.devicePixelRatio
          clientWidth = clientWidth >= 660 ? 660 : clientWidth
          // if ( isIPhone ) {
          //   devicePixelRatio = devicePixelRatio >= 3 ? 3 : devicePixelRatio
          // } else {
          //   devicePixelRatio = Math.floor(devicePixelRatio)
          // }
          devicePixelRatio = 2

          docEle.style.fontSize = Math.floor(devicePixelRatio * clientWidth / 10) + 'px'
        }

    win.addEventListener(resizeEvent, recalc, false)
    doc.addEventListener("DOMContentLoaded", recalc, false)
  }

  Main.prototype.carType2attr = function (key) {
    var dictList = this.dictList,
        carAttr = dictList.carAttr,
        carTypeAttrsRels = dictList.carTypeAttrsRels,
        carAttrsVo = [],
        data = []

    // 根据车长去获得车型字典
    for (var i = 0, len = carTypeAttrsRels.length; i < len; i++) {
      if (carTypeAttrsRels[i].carType == key) {
        carAttrsVo = carTypeAttrsRels[i].carAttrsVo
        break
      }
    }
    // 根据字典去解析
    // 获得对于的车型数据
    for (var j = 0, len2 = carAttrsVo.length; j < len2; j++) {
      for (var k = 0, len3 = carAttr.length; k < len3; k++) {
        if (carAttr[k].key == carAttrsVo[j]) {
          data.push(carAttr[k])
        }
      }
    }
    // 填充数据
    $('.search-cells-two .search-cell-items').html(template('tpl-cell', {
      data: data
    }))
  }

  // cell点击
  Main.prototype.cellsFn = function () {
    var that = this
    $('.search-cells-box').on('click', function (e) {
      var target = e.srcElement,
          $target = $(target),
          index = $target.index(),
          siblings = $target.siblings(),
          $this = $(this)

      if (target.tagName.toUpperCase() != 'LABEL') {
        return
      }

      siblings.removeClass('search-cell-item-select')
      $target.addClass('search-cell-item-select')
      var key = $target.attr('data-key') || ''

      if ($this.hasClass('search-cells-one')) {
        that.carType2attr(key)
        that.searchVal.cargoTypeCode = key
      }
      if ($this.hasClass('search-cells-two')) {
        that.searchVal.vechicleTypeCode = key
      }
      if ($this.hasClass('search-cells-three')) {
        that.searchVal.vechicleAttributeCode = key
      }
    })
  }

  Main.prototype.submit = function () {
    var that = this
    $('.search-footer-submit').on('click', function () {
      that.getList()
    })
  }

  Main.prototype.getList = function () {
    var that = this
    $('.loader').show()
    $.ajax({
      type: 'post',
      url: '/search',
      data: that.searchVal,
      success: function(res) {
        if (res.code == '1000') {
          var list = res.data.list
         if (list.length) {
          var html = template('tpl-list', { data: list })
          $('.result-list').html(html)
         }
        } else {
          alert('获取数据错误')
        }
        if (!that.initStatus) {
          that.initStatus = true
        } else {
          $('.search-container').fadeOut()
        }
        
        $('.loader').hide()
      },
      dataType: 'json',
      timeout: 10000,
      error: function(e) {
        alert('后台繁忙...')
        $('.loader').hide()
      }
    })
  }

  // 重置
  Main.prototype.resetFn = function () {
    var that = this
    $('.search-footer-reset').on('click', function () {
      $('.search-cells-one .search-cell-item')
        .removeClass('search-cell-item-select')
        .eq(0).addClass('search-cell-item-select')
      $('.search-cells-two .search-cell-item')
        .removeClass('search-cell-item-select')
        .eq(0).addClass('search-cell-item-select')
      $('.search-cells-three .search-cell-item')
        .removeClass('search-cell-item-select')
        .eq(0).addClass('search-cell-item-select')
      that.searchVal.cargoTypeCode = ''
      that.searchVal.vechicleTypeCode = ''
      that.searchVal.vechicleAttributeCode = ''
    })
  }

  Main.prototype.getDict = function () {
    var that = this
    $('.loader').show()
    $.ajax({
      type: 'get',
      url: '/get/dict',
      success: function(res) {
        if (res.code == '1000') {
          that.dictList = res.data

          $('.search-cells-three .search-cell-items').html(template('tpl-cell', {
            data: that.dictList.cargoTypeCode
          }))
          $('.search-cells-two .search-cell-items').html(template('tpl-cell', {
            data: that.dictList.cargoTypeCode
          }))
          $('.search-cells-one .search-cell-items').html(template('tpl-cell', {
            data: that.dictList.carType
          }))
        } else {
          alert('获取数据错误')
        }
        $('.loader').hide()
      },
      dataType: 'json',
      error: function(e) {
        $('.loader').hide()
      }
    })
  }

  Main.prototype.getTransportList = function () {
    var that = this
    $('.loader').show()
    $.ajax({
      type: 'get',
      url: '/transport/list',
      success: function(res) {
        if (res.code == '1000') {
          var transportList = res.data
          this.transportList = transportList
          for (var i = 0, len = transportList.length; i < len; i++) {
            var item = transportList[i]
            that.destinateAddressList = [{
              province: item.destinateAddressProvince,
              city: item.destinateAddressCity
            }]
            that.loadingAddressList = [{
              province: item.loadingAddressProvince,
              city: item.loadingAddressCity
            }]
          }
        } else {
          alert('获取数据错误')
        }
        $('.loader').hide()
      },
      dataType: 'json',
      error: function(e) {
        $('.loader').hide()
      }
    })
  }

  Main.prototype.init = function () {
    this.getTransportList()
    this.getList()
    this.rem()
    this.getDict()
    this.addrTabFn()
    this.changeAddr()
    this.headerTap()
    this.maskTap()
    this.searchBtnTap()
    this.cellsFn()
    this.resetFn()
    this.submit()
  }

  var main = new Main()
  main.init()
})()